package com.stars.home.utils

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.stars.home.R
import com.stars.home.inte.ChoiceListData
import com.stars.home.inte.ChoseTimeData
import com.stars.home.view.TimeDayAddHourView

/**
 * 选择时间 今天明天
 *
 *on 2020/11/21
 */
object ChoseLocationTime {


    fun show(context: Context,star_time:String?,end_time:String?, view: View, choiceCallData: ChoseTimeData) {
        var time = ""
        var timeTo = ""
        var timeTwo = ""
        var timeThree = ""
        var timeFour=""
        var timeFourTo=""

        var mMinuts=""
        var mMinutsTo=""

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_data_chose, null)
        val ll: LinearLayout = layout.findViewById(R.id.ll)
        val tvCancel: TextView = layout.findViewById(R.id.tv_cancel)
        val tvSure: TextView = layout.findViewById(R.id.tv_sure)
        val wheelView: TimeDayAddHourView = layout.findViewById(R.id.wheel_view)

       // wheelView.setData(day, daytime)


        time = wheelView.getTimeOne()//简化日期
        timeTo = wheelView.getTimeOneTo()//未简化日期
        timeTwo = wheelView.getTimeTwo()//时间
        timeThree = wheelView.getTimeThree()//时间

        timeFour= wheelView.getTimeEnd()//简化日期-结束
        timeFourTo=wheelView.getTimeEndTo()//未简化日期-结束


        mMinuts=wheelView.getMinuteStart()
        mMinutsTo=wheelView.getMinuteEnd()

        wheelView.setFlag()



        //设置已经编辑的默认时间
        if(null!=star_time&&!TextUtils.isEmpty(star_time)){
            var timeStat=star_time.split(" ")
             timeTo=timeStat[0]//简化的时间

            var ss=timeStat[1].split(":")
            timeTwo=ss[0]
            mMinuts=ss[1]


            wheelView.setTimeStart(timeTo,timeStat[1])

        }
        if(null!=end_time&&!TextUtils.isEmpty(end_time)){

            var timeEnd=end_time.split(" ")
            timeFourTo=timeEnd[0]

            var ss=timeEnd[1].split(":")
            timeThree=ss[0]
            mMinutsTo=ss[1]

            wheelView.setTimeEnd(timeFourTo,timeEnd[1])
        }





        //--------
        wheelView.setCallBack(object : ChoiceListData {
            override fun onRestuse(code: String?, name: String?) {
                if (code == "1") {
                    time = name!!
                    timeTo = wheelView.getTimeOneTo()

                    Log.e("-shy-", "timeTo=: $timeTo")

                }

                if (code == "2") {
                    timeTwo = name!!
                }

                if (code == "3") {
                    timeThree = name!!
                }

                if (code == "4") {

                    timeFour= name!!
                    timeFourTo=wheelView.getTimeEndTo()

                }

                if (code == "6") {

                    mMinuts= name!!
                }

                if (code == "7") {

                    mMinutsTo= name!!
                }


            }
        })

        tvSure.onClick {

//            var time1 = timeThree.split(":")
//            var time2 = timeTwo.split(":")
//
//            if (time1[0].toFloat() < time2[0].toFloat()) {
//                Toasts.toast(context, "截止时间不能小于起始时间")
//                return@onClick
//            }
            Log.e("-shy-", "time= $time")
            if (null != choiceCallData) {
                choiceCallData!!.onRestuse("$timeTo $timeTwo:$mMinuts", "$timeFourTo $timeThree:$mMinutsTo")
            }

            mNormalPopup?.dismiss()
        }


        tvCancel.onClick {
            mNormalPopup?.dismiss()
        }

        ll.onClick {

            mNormalPopup?.dismiss()
        }

        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)

        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_AUTO)
        mNormalPopup.show(view)


    }


}
